package com.ufox.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ufox.entity.*;
import com.ufox.exception.UserException;
import com.ufox.mapper.*;
import com.ufox.service.IOrdersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 优狐科技-www.hbufox.com
 * @since 2022-06-09
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements IOrdersService {

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private CartitemMapper cartitemMapper;

    @Autowired
    private OrderitemMapper orderitemMapper;

    @Autowired
    private ProductsMapper productsMapper;

    @Override
    public void addOrders(Orders orders) {
        ordersMapper.insert(orders);
        //2  订单项表 插入多条记录
        List<Cartitem> cartItems = cartitemMapper.findCartItemByUserId(orders.getUserId() + "");
        for(Cartitem cartItem : cartItems){
            Orderitem orderitem = new Orderitem();
            orderitem.setOrderId(orders.getId());
            orderitem.setProdId(cartItem.getProdId());
            orderitem.setBuynum(cartItem.getBuynum());
            orderitemMapper.insert(orderitem);

            Products products = productsMapper.selectById(cartItem.getProdId());
            //3 商品   相应商品减去库存
            try{
                productsMapper.updateProdNumByIdjian(cartItem.getBuynum()+"",cartItem.getProdId()+"");
            }catch (Exception e) {
                throw new UserException(products.getName()+"商品已无货，生成订单失败");
            }

        }
        //4  购物车表 根据用户id 清除记录
        LambdaQueryWrapper<Cartitem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cartitem::getUserId,orders.getUserId());
        cartitemMapper.delete(wrapper);
    }

    @Override
    public void deleteOrder(String id) {

        LambdaQueryWrapper<Orderitem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Orderitem::getOrderId,id);

        //商品数量 加回来
        List<Orderitem> orderitems = orderitemMapper.selectList(wrapper);
        for (Orderitem orderitem : orderitems) {
            Products products = new Products();
            products.setPnum(orderitem.getBuynum());
            LambdaQueryWrapper<Products> w = new LambdaQueryWrapper<>();
            w.eq(Products::getId,orderitem.getProdId());
            productsMapper.update(products,w);
        }

        //删除订单项表多条数据
        //注意顺序 先删除订单项中多条数据再删除订单表单列数据
        orderitemMapper.delete(wrapper);
        //相对未支付删除订单来说
        //删除订单表一条数据
        LambdaQueryWrapper<Orders> wrapperOrder = new LambdaQueryWrapper<>();
        wrapperOrder.eq(Orders::getId,id);
        ordersMapper.delete(wrapperOrder);

    }
}
